using System.Collections;
using UnityEngine;

public static class AnimationUtil
{
	private static void AddNewClip(this Animation animation, AnimationClip clip)
	{
		if (animation.GetClip(clip.name) == null)
		{
			animation.AddClip(clip, clip.name);
		}
	}

	public static void SetLength(this Animation animation, string anim, float newDuration)
	{
		animation[anim].speed = animation.GetClip(anim).length / newDuration;
	}

	public static void SafePlay(this Animation animation, AnimationClip clip)
	{
		animation.AddNewClip(clip);
		animation.Play(clip.name);
	}

	public static void SafeCrossFade(this Animation animation, AnimationClip clip, float fadeLength = 0.1f)
	{
		animation.AddNewClip(clip);
		animation.CrossFade(clip.name, fadeLength);
	}

	public static IEnumerator CrossFadeAndWait(this Animation animation, AnimationClip clip, float fadeLength = 0.1f)
	{
		animation.SafeCrossFade(clip, fadeLength);
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static IEnumerator PlayAndWait(this Animation animation)
	{
		animation.Play();
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static IEnumerator PlayAndWait(this Animation animation, string name)
	{
		animation.Play(name, PlayMode.StopAll);
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static IEnumerator PlayAndWait(this Animation animation, AnimationClip clip)
	{
		animation.SafePlay(clip);
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static IEnumerator PlayAndWait(this Animation animation, string clip, float fadeLength)
	{
		animation.CrossFade(clip, fadeLength);
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static IEnumerator PlayAndWait(this Animation animation, AnimationClip clip, float fadeLength)
	{
		animation.SafeCrossFade(clip, fadeLength);
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static IEnumerator WaitWhileIsplaying(this Animation animation)
	{
		yield return null;
		while (animation != null && animation.enabled && animation.isPlaying)
		{
			yield return null;
		}
	}

	public static void SampleInAnimationStart(this Animation animation)
	{
		animation.Stop();
		animation.Play();
		animation.Sample();
		animation.Stop();
	}

	public static void SampleInAnimationStart(this Animation animation, AnimationClip clip)
	{
		animation.AddNewClip(clip);
		animation.Stop();
		animation.Play(clip.name);
		animation.Sample();
		animation.Stop();
	}

	public static void SampleInAnimationStart(this Animation animation, string clipName)
	{
		animation.Stop();
		animation.Play(clipName);
		animation.Sample();
		animation.Stop();
	}

	public static void SampleInAnimationEnd(this Animation animation)
	{
		animation.SampleInNormalizedTime(animation.clip.name, 1f);
	}

	public static void SampleInAnimationEnd(this Animation animation, AnimationClip clip)
	{
		animation.AddNewClip(clip);
		animation.SampleInNormalizedTime(clip.name, 1f);
	}

	public static void SampleInAnimationEnd(this Animation animation, string clipName)
	{
		animation.SampleInNormalizedTime(clipName, 1f);
	}

	public static void SampleInNormalizedTime(this Animation animation, float normalizedTime)
	{
		animation.SampleInNormalizedTime(animation.clip.name, normalizedTime);
	}

	public static void SampleInNormalizedTime(this Animation animation, string name, float normalizedTime)
	{
		animation.PlayInNormalizedTime(name, normalizedTime);
		animation.Stop();
	}

	public static void PlayInNormalizedTime(this Animation animation, float normalizedTime)
	{
		animation.PlayInNormalizedTime(animation.clip.name, normalizedTime);
	}

	public static void PlayInNormalizedTime(this Animation animation, string name, float normalizedTime)
	{
		animation.Stop();
		animation.Play(name);
		animation[name].normalizedTime = normalizedTime;
		animation.Sample();
	}

	public static void PlayFromTime(this Animation animation, string name, float time)
	{
		float normalizedTime = time / animation[name].length;
		animation.PlayInNormalizedTime(name, normalizedTime);
	}
}
